package com.edu.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *此类功能也用作消息返回处理。
 *1.当用户登录成功之后，来到这个处理器。
 *2.在这里设置返回的消息。
 *3.只考虑 json 格式(因为登录只用 Ajax)。
 *4.需要继承 SavedRequest...Handler
 */
@Component
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication authc) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(
                "{\"result\":\"success\"}"
        );
        System.out.println("{登录消息处理器}登录成功");
    }
}
